// pages/tech/farm-note/list/list.js
//调用方式/pages/tech/farm-note/list/list?appid=1321"

var httpUtil = require('../../../../utils/httpUtil.js')
var uiUtil = require('../../../../utils/uiUtil.js')
var util = require('../../../../utils/util.js')
var emptyUtil = require('../../../templates/empty/empty.js')
const app = getApp()
var that

Page({

  /**
   * 页面的初始数据
   */
  data: {
    requestParam: { //搜索条件
      "appId": null,
      "pageNum": 0,
      "pageRows": 30,
    },
    enableLoadMore: true,
    isLoadingMore: false,

    emptyParam: {},

    dataList: null, //数据列表
    requestTask: null, //请求task
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    uiUtil.showLoading("正在加载..")
    var appId = options.appid
    if (!appId) { //不存在appid-退出
      wx.navigateBack({})
      return;
    }
    this.setData({
      'requestParam.appId': appId,
    })

    this.requestDataList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    return util.handleShareApp()
  },

  /**
   * 滑动到底部
   */
  onReachBottom: function() {
    this.requestDataList()
  },

  /**
   * 请求列表数据
   */
  requestDataList() {
    var requestTask = this.data.requestTask;
    if (requestTask) {
      try {
        requestTask.abort()
      } catch (e) {}
    }

    var curPage = that.data.requestParam.pageNum

    if (curPage != 0 && (!that.data.enableLoadMore || that.data.isLoadingMore)) { //不可加载||正在加载
      return;
    }

    this.setData({
      isLoadingMore: true,
      'requestParam.pageNum': curPage + 1
    })
    var requestTask = httpUtil.PostRequestHeaderApp("list_notebook", that.data.requestParam, {
      complete() {
        wx.hideLoading()
        that.setData({
          isLoadingMore: false
        })
      },
      success(res) {
        //整理数据
        var resList = res.list || []
        var resListLength = resList.length
        for (var i = 0; i < resListLength; i++) {
          var item = resList[i]
          var time = item.createTime
          time = time.replace(/-/g, '/')
          var date = new Date(time)
          var month = date.getMonth() + 1
          var day = date.getDate()
          item.tMonth = month + "月"
          item.tDay = day
        }

        var dataList;
        if (1 == that.data.requestParam.pageNum) {
          dataList = res.list || []
          if (dataList.length == 0) { //内容为空
            emptyUtil.showEmptyView()
          } else {
            emptyUtil.hideView()
          }
        } else {
          dataList = that.data.dataList.concat(res.list || [])
        }
        that.setData({
          dataList: dataList,
          enableLoadMore: dataList.length < res.rowCount,
        })
      },
      fail(code, res) {
        that.setData({
          enableLoadMore: false,
        })
        if (1 == that.data.requestParam.pageNum) { //内容为空
          var text = emptyUtil.getErrText(code, res, "获取数据失败")
          emptyUtil.showFailView(text)
        }
      }
    })
    that.setData({
      requestTask: requestTask
    })
  },

  /**
   * 条目点击
   */
  onItemClick(e) {
    var index = e.currentTarget.dataset.index
    var item = this.data.dataList[index]

    var appCode = item.appType //应用类型
    var appId = item.appId
    var recordId = item.recordId


    if ("app_field_service" == appCode) { //现场服务
      wx.navigateTo({
        url: '/pages/tech/field-service/detail/detail?appid=' + item.appId + '&recordid=' + recordId,
      })
    } else if ("app_fruit_check" == appCode) { //水果检测
      wx.navigateTo({
        url: '/pages/tech/fruit-check/detail/detail?appid=' + item.appId + '&recordid=' + recordId,
      })
    } else if ("app_soil_check" == appCode) { //土壤检测
      wx.navigateTo({
        url: '/pages/tech/soil-check/detail/detail?appid=' + item.appId + '&recordid=' + recordId,
      })
    } else if ("app_plan" == appCode) { //农事指导
      wx.navigateTo({
        url: '/pages/tech/farm-suggest/detail/detail?appid=' + item.appId + '&recordid=' + recordId,
      })
    } else {
      uiUtil.showToast("不支持的业务类型,请升级版本后重试")
    }
  },

  /**
   * 提交反馈成功
   */
  onResultFeedBackOk(recordId, appId) {
    try {
      var dataList = this.data.dataList
      for (var i = 0; i < dataList.length; i++) {
        var item = dataList[i]
        if (appId == item.appId && recordId == item.recordId) {
          item.showBtn = '1'
        }
      }
      this.setData({
        dataList: dataList
      })
    } catch (e) {}

  },

})